import {BasicColumn, FormSchema} from '/@/components/Table';
import {render} from '/@/utils/common/renderUtils';
import {DescItem} from '/@/components/Description/index';
import {ChannelTypeEnum} from './template.model';
import {JVxeColumn, JVxeTypes} from '/@/components/jeecg/JVxeTable/types'
import {listTask} from '../task/task.api';

export const columns: BasicColumn[] = [
  {
    title: '任务名称',
    dataIndex: 'taskName',
    width: 120,
  }, 
  {
    title: '任务编码',
    dataIndex: 'taskCode',
    width: 120,
  },   
  {
    title: '模板标题',
    dataIndex: 'title',
    helpMessage: '默认为任务名称',
    width: 120,
  },  
  {
    title: '渠道类型',
    dataIndex: 'channelType',
    width: 120,
    customRender: ({text}) => {
      let color = "green"
      return render.renderTag(render.renderDict(text, 'message_channel_type'), color)
    },
  },

  {
    title: '创建时间',
    dataIndex: 'createdAt',
    sorter: true,
    width: 160
  },
  {
    title: '创建者',
    dataIndex: 'createdBy',
    sorter: true,
    width: 120
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 60,
    customRender: ({ text }) => {
      const color = text ? 'green' : 'red';
      return render.renderTag(text ? '启用' : '禁用', color)
    }
  },
];

export const taskTemplateColumns: BasicColumn[] = [
  {
    title: '渠道类型',
    dataIndex: 'channelType',
    width: 120,
    align: 'left',
    customRender: ({text}) => {
      let color = "green"
      return render.renderTag(render.renderDict(text, 'message_channel_type'), color)
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 64,
    slots: { customRender: 'status'},
    // customRender: ({ text }) => {
    //   return render.renderSwitch(text, 'data_use_status')
    // }
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'taskId',
    label: '推送任务',
    component: 'ApiSelect',
    componentProps: ({ formActionType }) => {
      return {
        optionFilterProp: "label",
        showSearch: true,
        labelField: 'name',
        valueField: 'id',
        api: listTask,
      }
    }
  },
  {
    label: '渠道类型',
    field: 'channelType',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'message_channel_type',
      showSearch: true
    },
    colProps: {span: 6},
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: true },
        { label: '禁用', value: false },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'id',
    component: 'Input',
    show: false,
    required: false
  },
  // {
  //   field: 'name',
  //   label: '名称',
  //   component: 'Input',
  //   required: true
  // },
  {
    field: 'isShowSelectTask',
    label: 'isShowSelectTask',
    component: 'RadioGroup',
    show: false,
    required: false
  },
  {
    field: 'taskId',
    label: '推送任务',
    component: 'ApiSelect',
    show: ({ model, field }) => model['isShowSelectTask'],
    required: true,
    componentProps: ({ formActionType }) => {
      return {
        optionFilterProp: "label",
        showSearch: true,
        labelField: 'name',
        valueField: 'id',
        api: listTask,
      }
    }
  },
  {
    field: 'title',
    label: '模板标题',
    helpMessage: '默认为任务名称',
    component: 'Input'
  },

  {
    field: 'channelType',
    label: '渠道类型',
    ifShow: ({ values }) => values.id,
    component: 'Input',
    render: ({ model, field }) => {
      return render.renderTag(render.renderDict(model[field], 'message_channel_type'), 'green')
    },
  },

  {
    label: '渠道类型',
    field: 'channelType',
    required: true,
    component: 'JDictSelectTag',
    ifShow: ({ values }) => !values.id,
    componentProps: {
      type: "radio",
      dictCode: 'message_channel_type',
      showSearch: true
    }
  },

  {
    field: 'status',
    label: '状态',
    defaultValue: true,
    required: true,
    component: 'RadioGroup',
    componentProps: {
      options: [
        { label: '启用', value: true },
        { label: '禁用', value: false },
      ],
    },
  },

  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入备注',
      rows: 5,
    },
  }
];


export const formDetailSchema: DescItem[] = [
  {
    field: 'taskName',
    label: '任务名称',
  },
  {
    field: 'channelTypeName', 
    label: '消息渠道',
    // render: (curVal, data) => {
    //   const {channelId} = data
    //   return render.renderJumpRoute({
    //     query: {id: channelId}, title: curVal, path: '/msg/channel/index'
    //   })
    // },
  },

  {
    field: 'status',
    label: '状态',
    render: (curVal, data) => {
      const color = curVal ? 'green' : 'red';
      return render.renderTag(curVal ? '启用' : '禁用', color)
    }
  },
  {
    field: 'createdAt',
    label: '创建时间'
  },
  {
    field: 'createdBy',
    label: '创建者'
  },
  {
    field: 'updatedAt',
    label: '更新时间'
  },
  {
    field: 'updatedBy',
    label: '更新者'
  },
  {
    label: '备注',
    labelMinWidth: 40,
    field: 'remark',
  }
];



export const channelColumns: BasicColumn[] = [
  {
    title: '渠道类型',
    dataIndex: 'channelTypeName',
    width: 100,
  },
  {
    title: '渠道名称',
    dataIndex: 'channelName',
    width: 160,
  }
];

export const templateSchema: FormSchema[] = [
  {
    // 小程序或者PC推送才拥有
    field: 'pageUrl',
    label: '页面地址',
    helpMessage: '点击推送的消息将会跳转到该地址',
    required: false,
    ifShow: ({ values }) => {
      return values.channelType === ChannelTypeEnum.PC || 
             values.channelType === ChannelTypeEnum.WX_MA || 
             values.channelType === ChannelTypeEnum.WX_MP
    },
    component: 'Input',
  },
  {
    field: 'channelType',
    label: 'channelType',
    show: false,
    component: 'Input',
  },
  {
    // PC推送或者APP推送才拥有
    field: 'mediaId',
    label: '媒体文件',
    helpMessage: '推送的时候会以链接形式推送给目标, 需要从素材库中选择',
    required: false,
    ifShow: ({ values }) => {
      return values.channelType === ChannelTypeEnum.PC
    },
    component: 'MySelectMaterial',
  },
  {
    field: 'templateId',
    label: '模板ID',
    ifShow: ({ values }) => {
      return values.channelType !== ChannelTypeEnum.EMAIL && values.channelType !== ChannelTypeEnum.PC
    },
    required: ({ values }) => values.channelType !== ChannelTypeEnum.EMAIL && values.channelType !== ChannelTypeEnum.PC,
    component: 'Input',
  },
];


/**
 * 模板变量
 */
export const templateVarColumns : JVxeColumn[] = [
  // 这里实际是task中定义的字段名称
  { title: 'fieldName', key: 'fieldName', type: JVxeTypes.hidden },
  { 
    title: '字段描述', key: 'desc', type: JVxeTypes.normal, width: 200, defaultValue: '', disabled: true
  },
  { 
    title: '字段键', key: 'key', type: JVxeTypes.input, width: 160, defaultValue: '',
    validateRules: [{ required: true, message: '${title}不能为空' }],
  },

  { 
    title: '字段值', key: 'value', type: JVxeTypes.input, width: 180, defaultValue: '',
    validateRules: [{ required: false, message: '${title}不能为空' }],
  },

  { 
    title: '字段颜色', key: 'color', type: JVxeTypes.input, defaultValue: ''
  },
]
